package com.wc.AlgoOJ_homework.aloj练习赛.G_涂墙;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/8 22:35
 * @description http://43.138.190.70:8888/p/GDCPC7
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010;
    // op存储所有操作
    static int[][] op = new int[N][3];
    // row[i]表示操作第i行的是第row[i]次操作，后面的操作会覆盖前面的操作
    static int[] row = new int[N];
    // col[i]表示操作第i列的是第col[i]次操作
    static int[] col = new int[N];
    // nums[i]表示颜色为i的有nums[i]种
    static long[] nums = new long[N];
    static int h, w, m;

    public static void main(String[] args) {
        // R表示后面操作行的次数，C表示后面操作列的次数，RR表示后面操作行的同一行的重复次数，RC表示后面操作列同一列的重复次数
        int R = 0, C = 0, RR = 0, RC = 0;
        h = sc.nextInt();
        w = sc.nextInt();
        m = sc.nextInt();
        Arrays.fill(row, 1, h + 1, -1);
        Arrays.fill(col, 1, w + 1, -1);

        for (int i = 1; i <= m; i++) {
            op[i][0] = sc.nextInt();
            op[i][1] = sc.nextInt();
            op[i][2] = sc.nextInt();

            if (op[i][0] == 1) {
                R++;
                if (row[op[i][1]] != -1) {
                    RR++;
                }
                row[op[i][1]] = i;
            } else {
                C++;
                if (col[op[i][1]] != -1) {
                    RC++;
                }
                col[op[i][1]] = i;
            }
        }
        long sum = (long) h * w;

        for (int i = 1; i <= m; i++) {
            if (op[i][0] == 1) {
                // 是否被覆盖，覆盖了不需要算，因为我们是存储的最后一次操作
                if (row[op[i][1]] == i) {
                    // 计算后面竖着的放了多少个，会污染
                    nums[op[i][2]] += w - (C - RC);
                } else {
                    RR--;
                }
                R--;
            } else {
                // 是否被覆盖，覆盖了不需要算
                if (col[op[i][1]] == i) {
                    // 计算后面横着的放了多少个，会污染
                    nums[op[i][2]] += h - (R - RR);
                } else {
                    RC--;
                }
                C--;
            }
        }
        int n = 0;
        for (int i = 1; i < N; i++) {
            if (nums[i] != 0) {
                n++;
                sum -= nums[i];
            }
        }
        nums[0] = sum;
        if (sum != 0) n++;
        out.println(n);
        for (int i = 0; i < N; i++) {
            if (nums[i] != 0) out.println(i + " " + nums[i]);
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
